<template>
  <div>
    <el-dialog :title="$root.lang.control.device.detail.app.logs.historyTitle" :visible.sync="showDialog" width="50%" @close="closeDialog" :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form ref="form" :model="form" :rules="rules" size="small" label-position="right" label-width="110px">
        <el-form-item :label="$root.lang.control.device.detail.app.logs.isNew" prop="isNew">
          <el-radio-group v-model="form.isNew">
							<el-radio :label="item.value" :key="index" :value="item.value" v-for="(item, index) in baseState.isNew" >{{ item.title }}</el-radio>
						</el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button class="dialog-btn" :loading="loading" type="primary" @click="onSubmit('form')">{{ $root.lang.control.device.detail.app.logs.confirm }}</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import '@/options/baseState';
export default {
  data() {
    return {
      loading: false,
      showDialog: this.show,
      form: {
        isNew: true,
      },
      baseState: baseState,
      rules: {
        isNew: [{ required: true, message: this.$root.lang.control.device.detail.app.logs.rule.isNew, trigger: 'blur' }],
      }
    }
  },
  props: {
    show: {
      type: Boolean,
      default: false
    },
    deviceId: {
      type: String,
      default: null
    },
    appId: {
      type: String,
      default: null
    },
  },
  watch: {
    show: function (value) {
      if(this.$refs['form']) this.$refs['form'].resetFields()
      this.showDialog = value
    }
  },
  methods: {
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.doSet()
        }
      })
    },
    doSet() {
      this.loading = true
      this.request({
        url: '/daliy/history/update?appId=' + this.appId + '&deviceId=' + this.deviceId + '&isNew=' + this.form.isNew,
        method: 'get',
      }).then(response => {
        this.loading = false
        this.$parent.uniqueId = response.result
        this.showDialog = false
        this.$message({
          message: '操作成功，请稍后导出日志',
          type: 'success',
          duration: 3 * 1000
        })
      }).catch(error => {
        this.loading = false
        this.showDialog = false
        this.$message({
          message: error.errmsg,
          type: 'error',
          duration: 3 * 1000
        })
      })
    },
    closeDialog() {
      this.$emit('editReceive',{
        key: 'showLogSetting',
        value: this.showDialog
      })
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">

</style>